<?php
	class pelicula_modelo extends CI_Model{
		function InsertarPelicula($titulo, $anyo, $genero, $sinopsis, $rutaPoster, $rutaVideo){
			$data = array(
			'titulo'	 	=> $titulo,
			'anyo' 			=> $anyo,
			'genero'		=> $genero,
			'sinopsis'		=> $sinopsis,
			'rutaPoster'	=> $rutaPoster,
			'rutaVideo'		=> $rutaVideo
			);
			
			$this->db->select('*')->from('pelicula')->where('titulo',$titulo);
			$query = $this->db->get();
			if($query->num_rows>0){
				return FALSE;
			}
			
			//else{
			$this->db->insert('pelicula', $data); 
			return TRUE;
			//}
		}
		
	function EliminarPelicula($id){
		$this->db->where('id', $id);
		$this->db->delete('pelicula'); 
		}
	
	
	function ListarPelicula($pagina,$quehago){
		switch($quehago){
            case 'limit':
                $query=$this->db
                ->select("*")
                ->from("pelicula")
                ->order_by("titulo","asc")
                ->limit(10,$pagina)
                ->get();
                return $query->result();
            break;
            case 'cuantos':
                 $query=$this->db
                ->select("*")
                ->from("pelicula")
                ->order_by("titulo","asc")
                ->count_all_results();
                return $query;
            break;
        }
			
		}
		
	function ListarPeliculaTitulo($busqueda,$limite,$offset){
			$this->db->select('*')->from('pelicula')->like('titulo',$busqueda)->limit($limite,$offset);
			$query = $this->db->get();
			if($query->num_rows==0){
				
			}
			else{
				$result=$query->result();
				return $result;
			}
		}

	function ListarPeliculaGeneroCarrusel($busqueda,$limite,$offset){
			$this->db->select('*')->from('pelicula')->like('genero',$busqueda)->limit($limite,$offset);
			$query = $this->db->get();
			if($query->num_rows==0){
				
			}
			else{
				$result=$query->result();
				return $result;
			}
		}
	
	function ListarPeliculaGenero($genero,$pagina,$quehago){
			switch($quehago){
		case 'limit':
			$query=$this->db
			->select("*")
			->from("pelicula")
			->where('genero',$genero)
			->order_by("titulo","asc")
			->limit(10,$pagina)
			->get();
			return $query->result();
		break;
		case 'cuantos':
			 $query=$this->db
			->select("*")
			->from("pelicula")
			->where('genero',$genero)
			->order_by("titulo","asc")
			->count_all_results();
			return $query;
		break;
        }
		
		}
		
	function EditarPelicula($id, $titulo, $anyo, $genero, $sinopsis){
		$this->db->select('*')->from('pelicula')->where('id',$id);

		$data = array(
			'titulo'	 	=> $titulo,
			'anyo' 			=> $anyo,
			'genero'		=> $genero,
			'sinopsis'		=> $sinopsis
		);	
		$this->db->update('pelicula', $data);
		
	}
	
	function BuscarPelicula($busqueda,$limit,$offset){
			$this->db->select('*')->from('pelicula')->like('titulo',$busqueda)->limit($limit,$offset);
			$query = $this->db->get();
			if($query->num_rows==0){
				
			}
			else{
				$result=$query->result();
				return $result;
			}
		}
		
	function ObtenerPelicula($id){
		$this->db->select('*')->from('pelicula')->where('id',$id);
		$query = $this->db->get();
		if($query->num_rows==0){
			
		}
		else{
			$result=$query->result();
			return $result;
		}
	}
		
	function ListarPeliculasRecientes($limite){
		$this->db->select('*')->from('pelicula')->order_by("fecha", "desc")->limit($limite);
		$query = $this->db->get();
		if($query->num_rows==0){
			
		}
		else{
			$result=$query->result();
			return $result;
		}
	}	
}
?>
